<?php

uses('sanitize');

class CommentsController extends AppController{
	var $name = 'Comments';
	var $helpers = array('Html2', 'Form', 'Javascript', 'Partial', 'ajax');
	var $components = array('RequestHandler', 'Textual');
	var $paginate = array('limit' => 5, 'order' => array('Page.created' => 'desc'));

//admin functions

	function admin_index(){
		$this->pageTitle = 'Admin Comments';
		$this->set('comments', $this->paginate());
		
	}

	function admin_edit($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Page');
			$this->redirect('/admin/comments/index');
		}
		//$this->cleanUpFields('Page')
				
		if($this->RequestHandler->isPost()){
			
			
			$this->__sanitize();

			//add notice moderated on update 
			$this->data['Comment']['content'] .= '<p class="notice"><small>[moderated]</small></p>';
			
			if($this->Comment->save($this->data)) {
				$this->Session->setFlash('The Comment has been updated');
				$this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}else{
			$this->data = $this->Comment->read(null, $id);
		}		
	}

	function admin_delete($id){
		//if($this->RequestHandler->isDelete()){ //voir html pour restfull
		if($this->RequestHandler->isPost()){
			$comment = $this->Comment->findById($id);
			$page_id = $comment['Comment']['page_id'];
			
			$this->Comment->del($id);
			$this->Comment->com_count($page_id);
			
			$this->Session->setFlash('The Comment with id : '.$id.' has been deleted.');
		}else{
			$this->Session->setFlash('Error');
		}
		$this->redirect('/admin/comments');
	}
	
	/* private */
	
	function __sanitize(){
		$san = new Sanitize();
		
		$this->data['Comment']['author'] = $san->stripAll($this->data['Comment']['author']);
		//supposed admin don't hack himself
		//$this->data['Page']['content'] = $san->escape($this->data['Page']['content']);
		//debug($this->data['Page']['created']);
		
	}
}
?>
